package com.bakery.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bakery.common.Result;
import com.bakery.dto.EmployeeDTO;
import com.bakery.dto.LoginDTO;
import com.bakery.entity.Employee;
import com.bakery.service.EmployeeService;
import com.bakery.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 员工Controller
 *
 * @author bakery
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    
    @Autowired
    private EmployeeService employeeService;
    
    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@Validated @RequestBody LoginDTO dto) {
        LoginVO loginVO = employeeService.login(dto);
        return Result.success(loginVO);
    }
    
    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        employeeService.logout();
        return Result.success();
    }
    
    /**
     * 获取当前登录员工信息
     */
    @GetMapping("/current")
    public Result<Employee> getCurrentEmployee() {
        Employee employee = employeeService.getCurrentEmployee();
        employee.setPassword(null); // 隐藏密码
        return Result.success(employee);
    }
    
    /**
     * 分页查询员工
     */
    @GetMapping("/page")
    public Result<Page<Employee>> getEmployeePage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) Integer status) {
        Page<Employee> employeePage = employeeService.getEmployeePage(page, size, keyword, role, status);
        return Result.success(employeePage);
    }
    
    /**
     * 新增员工（仅店长可操作）
     */
    @PostMapping
    @SaCheckRole("MANAGER")
    public Result<Void> addEmployee(@Validated @RequestBody EmployeeDTO dto) {
        employeeService.addEmployee(dto);
        return Result.success();
    }
    
    /**
     * 更新员工
     */
    @PutMapping
    public Result<Void> updateEmployee(@Validated @RequestBody EmployeeDTO dto) {
        employeeService.updateEmployee(dto);
        return Result.success();
    }
    
    /**
     * 删除员工（仅店长可操作）
     */
    @DeleteMapping("/{id}")
    @SaCheckRole("MANAGER")
    public Result<Void> deleteEmployee(@PathVariable Long id) {
        employeeService.deleteEmployee(id);
        return Result.success();
    }
    
    /**
     * 修改密码
     */
    @PostMapping("/changePassword")
    public Result<Void> changePassword(@RequestBody Map<String, String> params) {
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        employeeService.changePassword(oldPassword, newPassword);
        return Result.success();
    }
}

